package com.example.lenovo.demob;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class EditStusActivity extends AppCompatActivity {
    private EditText edit_no,edit_name,edit_age,edit_sex;
    private Button modify;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_stus);
        //定义组件
        findViews();
        //初始化输入框信息
        initData();
        //修改学生信息
        ModifyStus();
    }

    /**
     *   修改学生信息
     */
    private void ModifyStus() {
        modify=findViewById(R.id.btn_modify);
        modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String no=edit_no.getText().toString();
                String name=edit_name.getText().toString();
                int age=Integer.parseInt(edit_age.getText().toString());
                String sex=edit_sex.getText().toString();
                Intent intent=new Intent();
                intent.putExtra("NO",no);
                intent.putExtra("NAME",name);
                intent.putExtra("AGE",age);
                intent.putExtra("SEX",sex);
                setResult(3001,intent);
                finish();
            }
        });
    }

    /**
     *   初始化输入框信息
     */
    private void initData() {
        String no=getIntent().getStringExtra("NO");
        String name=getIntent().getStringExtra("NAME");
        String sex=getIntent().getStringExtra("SEX");
        int age=getIntent().getIntExtra("AGE",0);
        edit_no.setText(no);
        edit_name.setText(name);
        edit_sex.setText(sex);
        edit_age.setText(String.valueOf(age));

    }

    /**
     *  定义组件
     */
    private void findViews() {
        edit_no=findViewById(R.id.edit_no);
        edit_name=findViewById(R.id.edit_name);
        edit_age=findViewById(R.id.edit_age);
        edit_sex=findViewById(R.id.edit_sex);

    }
}
